/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.manual.controller;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.annotation.rest.AnonymousGetMapping;
import com.fzhucc.annotation.rest.AnonymousPostMapping;
import com.fzhucc.modules.activity.domain.dto.ActivityDto;
import com.fzhucc.modules.manual.domain.RaceManual;
import com.fzhucc.modules.manual.domain.dto.RaceManualDto;
import com.fzhucc.modules.manual.service.RaceManualService;
import com.fzhucc.modules.manual.domain.dto.RaceManualQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2023-08-16
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "大赛电子手册:管理")
@RequestMapping("/api/raceManual")
public class RaceManualController {

    private final RaceManualService raceManualService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('raceManual:list')")
    public void exportRaceManual(HttpServletResponse response, RaceManualQueryCriteria criteria) throws IOException {
        raceManualService.download(raceManualService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询manual")
    @ApiOperation("查询manual")
    @PreAuthorize("@el.check('raceManual:list')")
    public ResponseEntity<Object> queryRaceManual(RaceManualQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(raceManualService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增manual")
    @ApiOperation("新增manual")
    @PreAuthorize("@el.check('raceManual:add')")
    public ResponseEntity<Object> createRaceManual(@Validated @RequestBody RaceManual resources){
        return new ResponseEntity<>(raceManualService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改manual")
    @ApiOperation("修改manual")
    @PreAuthorize("@el.check('raceManual:edit')")
    public ResponseEntity<Object> updateRaceManual(@Validated @RequestBody RaceManual resources){
        raceManualService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除manual")
    @ApiOperation("删除manual")
    @PreAuthorize("@el.check('raceManual:del')")
    public ResponseEntity<Object> deleteRaceManual(@RequestBody Integer[] ids) {
        raceManualService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousGetMapping(value = "/show")
    @Log("小程序展示大赛电子手册")
    @ApiOperation("小程序展示大赛电子手册")
    public ResponseEntity<Object> show(RaceManualQueryCriteria criteria, Pageable pageable){
        criteria.setIsShow(1);
        return new ResponseEntity<>(raceManualService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/{id}")
    @Log("查询单个大赛电子手册")
    @ApiOperation("查询单个大赛电子手册")
    public RaceManualDto queryRaceManualById(@PathVariable Integer id) {
        return raceManualService.findById(id);
    }
}